package com.itcast.springboot.service.impl;

import com.itcast.springboot.bean.GiveLike;
import com.itcast.springboot.bean.InsertGiveLike;
import com.itcast.springboot.mapper.GiveLikeMapper;
import com.itcast.springboot.service.GiveLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
//对抛出的任何异常都进行自动回滚
@Transactional(rollbackFor = Exception.class)
public class GiveLikeServiceImpl implements GiveLikeService {

    @Autowired
    GiveLikeMapper givelikeMapper;

    @Override
    public InsertGiveLike insertLike(GiveLike like) {
        givelikeMapper.insertLike(like);
        if (like.getLid() != 0) {
            Integer count = givelikeMapper.getLikeNumByUserDynaId(like.getUserdynaId());
            InsertGiveLike igl = new InsertGiveLike(like.getLid(), count, "true");
            return igl;
        }
        return null;
    }

    @Override
    public InsertGiveLike deleteLike(Integer udid, String uid) {
//        //获取like表基本信息
//        GiveLike giveLike = givelikeMapper.getLikeById(lid);
        Integer deleteResult = givelikeMapper.deleteLike(udid, uid);
        if (deleteResult != 0) {
            Integer count = givelikeMapper.getLikeNumByUserDynaId(udid);
            InsertGiveLike igl = new InsertGiveLike(null, count, "false");
            return igl;
        }
        return null;
    }

    @Override
    public GiveLike getLikeById(Integer lid) {
        return givelikeMapper.getLikeById(lid);
    }

    @Override
    public List<Integer> getLikeNumsByUserDynaIds(List<Integer> udids) {
        return givelikeMapper.getLikeNumsByUserDynasId(udids);
    }

    @Override
    public List<String> isLikeByUdids(String uid, List<Integer> udids) {
        //存在点赞列表中的用户动态id
        List<Integer> existUdids = givelikeMapper.isExistByUdids(uid, udids);
        System.out.println(existUdids);
        for (Integer existUdid :
                existUdids) {
            System.out.println(existUdid);
        }
        //存在点赞列表中的用户动态id是否被点赞
        List<String> islikes = givelikeMapper.isLikeByUdids(uid, udids);
        for (String isLike :
                islikes) {
            System.out.println(isLike);
        }
        List<String> likes = new ArrayList<String>();
        int i = 0;
        for (i = 0; i < existUdids.size() - 1; i++) {
            //有两个相同的就存为true
            if (existUdids.get(i) == existUdids.get(i + 1)) {
                likes.add("true");
                i++;
                continue;
            }
            //保存原来的值
            likes.add(islikes.get(i));
        }
        if (i == existUdids.size() - 1) {
            //保存最后一个
            likes.add(islikes.get(islikes.size() - 1));
        }
        return likes;
    }

    @Override
    public List<GiveLike> getLikesAndUsersByUserDynasId(List<Integer> udids) {
        return givelikeMapper.getLikesAndUsersByUserDynasId(udids);
    }

    @Override
    public List<GiveLike> getGiveLikesAndNamesByIds(List<Integer> udids) {
        return givelikeMapper.getGiveLikesAndNamesByIds(udids);
    }

    @Override
    public Integer updateLike(GiveLike like) {
        return givelikeMapper.updateLike(like);
    }
}
